home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 September / Chip_2000-09_cd1.bin / sharewar / Slunec / app / 16 / EXEC.SWG / 0011_WINSDK Executables Information.pas < prev    next >
Pascal/Delphi Source File  |  1997-05-11  |  24KB  |  605 lines

  1. INF: Executable-File Header Format                            [P_WinSDK]
  2.  
  3. 3.00
  4. WINDOWS
  5. PSSONLY | Windows 3 Developers Notes softlib ENDUSER
  6.  
  7. Summary:
  8.  
  9. Note:  This  article  is part of  a  set of seven articles, collectively
  10. called  the "Windows 3.00 Developer's Notes." More information about the
  11. contents  of the other articles, and procedures for ordering a hard-copy
  12. set, can be found in the knowledge base article titled "INF: The Windows
  13. 3.00 Developer's Notes" (Q65260).
  14.  
  15. This  article can be found in  the Software/Data Library by searching on
  16. the  word  EXEFMT  or  S12688.  EXEFMT  was  archived  using  the PKware
  17. file-compression utility.
  18.  
  19. More Information:
  20.  
  21. Microsoft  defined  the  segmented executable  file  format  for Windows
  22. applications and dynamic-link libraries (DLLs). This file format is also
  23. referred  to  as  the  New  Executable  Format.  This  new  format is an
  24. extension  of  the existing MS-DOS  .EXE  format (old-style format). The
  25. purpose of the segmented executable format is to provide the information
  26. needed  to support the dynamic  linking and segmentation capabilities of
  27. the Windows environment.
  28.  
  29. An  executable file contains Microsoft Windows code and data, or Windows
  30. code,  data,  and  resources.  Specific fields  have  been  added to the
  31. old-style  .EXE format header to indicate the existence of the segmented
  32. file  format.  The  old-style  header  may  contain  a  valid executable
  33. program,  called a stub program, that will be executed if the program is
  34. run  on  MS-DOS  (without Windows). This  stub  program usually prints a
  35. message  indicating  that  Microsoft  Windows  is  required  to  run the
  36. program.  The  segmented executable format  extensions also begin with a
  37. header  that describes the contents and location of the executable image
  38. in  the file. The loader uses this  header information when it loads the
  39. executable segments in memory.
  40.  
  41.  
  42. ======================================================================
  43.                      OLD-STYLE HEADER EXTENSIONS
  44. ======================================================================
  45.  
  46. The  old-style header contains information the  loader expects for a DOS
  47. executable  file.  It describes a stub  program (WINSTUB) the loader can
  48. place  in memory when necessary, it  points to the new-style header, and
  49. it contains the stub programs relocation table.
  50.  
  51. The following illustrates the distinct parts of the old-style executable
  52. format:
  53.  
  54.         +-------------------------+
  55.     00h |  Old-style header info  |
  56.         +-------------------------+
  57.     20h |        Reserved         |
  58.         +-------------------------+
  59.     3Ch |   Offset to segmented   |
  60.         |       .EXE header       |
  61.         +-------------------------+
  62.     40h |  Relocation table and   |
  63.         |    DOS stub program     |
  64.         +-------------------------+
  65.         |  Segmented .EXE Header  |
  66.         |           .             |
  67.         |           .             |
  68.         |           .             |
  69.  
  70. The  word  at  offset  18h in  the  old-style  .EXE  header contains the
  71. relative  byte  offset to the stub  program's  relocation table. If this
  72. offset  is 40h, then the double word at  offset 3Ch is assumed to be the
  73. relative  byte offset from the beginning of the file to the beginning of
  74. the segmented executable header. A new-format .EXE file is identified if
  75. the  segmented  executable  header contains  a  valid  signature. If the
  76. signature  is  not valid, the file is  assumed to be an old-style format
  77. .EXE  file. The remainder of the old-style format header will describe a
  78. DOS  program,  the  stub.  The stub may  be  any  valid program but will
  79. typically be a program that displays an error message.
  80.  
  81. ======================================================================
  82.                          SEGMENTED EXE FORMAT
  83. ======================================================================
  84.  
  85. Because  Windows  executable  files are  often  larger  than one segment
  86. (64K),  additional  information (that does  not  appear in the old-style
  87. header)  is required so that the  loader can load each segment properly.
  88. The  segmented EXE format was developed  to provide the loader with this
  89. information.
  90.  
  91. The segmented .EXE file has the following format:
  92.  
  93.         +-----------------+
  94.     00h |  Old-style EXE  |
  95.         |      Header     |
  96.         +-----------------+
  97.     20h |    Reserved     |
  98.         +-----------------+
  99.     3Ch |    Offset to    | ---+
  100.         | Segmented Header|    |
  101.         +-----------------+    |
  102.     40h | Relocation Table|    |
  103.         |  & Stub Program |    |
  104.         +-----------------+    |
  105.         |                 |    |
  106.         +-----------------+    |
  107.     xxh |  Segmented EXE  | <--+
  108.         |      Header     |
  109.         +-----------------+
  110.         |  Segment Table  |
  111.         +-----------------+
  112.         | Resource Table  |
  113.         +-----------------+
  114.         |  Resident Name  |
  115.         |      Table      |
  116.         +-----------------+
  117.         | Module Reference|
  118.         |      Table      |
  119.         +-----------------+
  120.         | Imported Names  |
  121.         |      Table      |
  122.         +-----------------+
  123.         |   Entry Table   |
  124.         +-----------------+
  125.         |  Non-Resident   |
  126.         |   Name Table    |
  127.         +-----------------+
  128.         |   Seg #1 Data   |
  129.         |   Seg #1 Info   |
  130.         +-----------------+
  131.                 .
  132.                 .
  133.                 .
  134.         +-----------------+
  135.         |   Seg #n Data   |
  136.         |   Seg #n Info   |
  137.         +-----------------+
  138.  
  139.  
  140. The  following sections describe each of the components that make up the
  141. segmented  EXE  format.  Each  section  contains  a  description  of the
  142. component and the fields in the structures that make up that component.
  143.  
  144. Note:  All  unused fields and flag bits  are reserved for future use and
  145. must contain 0 (zero) values.
  146.  
  147. ======================================================================
  148.                          SEGMENTED EXE HEADER
  149. ======================================================================
  150.  
  151. The segmented EXE header contains general information about the EXE file
  152. and contains information on the location and size of the other sections.
  153. The  Windows loader copies this section, along with other data, into the
  154. module  table in the system data. The module table is internal data used
  155. by  the loader to manage the loaded executable modules in the system and
  156. to support dynamic linking.
  157.  
  158. The  following describes the format  of the segmented executable header.
  159. For  each  field, the offset is given  relative  to the beginning of the
  160. segmented header, the size of the field is defined, and a description is
  161. given.
  162.  
  163.     Offset Size Description
  164.     ------ ---- -----------
  165.  
  166.     00h     DW  Signature word.
  167.                 "N" is low-order byte.
  168.                 "E" is high-order byte.
  169.  
  170.     02h     DB  Version number of the linker.
  171.  
  172.     03h     DB  Revision number of the linker.
  173.  
  174.     04h     DW  Entry Table file offset, relative to the beginning of
  175.                 the segmented EXE header.
  176.     06h     DW  Number of bytes in the entry table.
  177.  
  178.     08h     DD  32-bit CRC of entire contents of file.
  179.                 These words are taken as 00 during the calculation.
  180.  
  181.     0Ch     DW  Flag word.
  182.                 0000h = NOAUTODATA
  183.                 0001h = SINGLEDATA (Shared automatic data segment)
  184.                 0002h = MULTIPLEDATA (Instanced automatic data
  185.                         segment)
  186.                 2000h = Errors detected at link time, module will not
  187.                         load.
  188.                 8000h = Library module.
  189.                         The SS:SP information is invalid, CS:IP points
  190.                         to an initialization procedure that is called
  191.                         with AX equal to the module handle. This
  192.                         initialization procedure must perform a far
  193.                         return to the caller, with AX not equal to
  194.                         zero to indicate success, or AX equal to zero
  195.                         to indicate failure to initialize. DS is set
  196.                         to the library's data segment if the
  197.                         SINGLEDATA flag is set. Otherwise, DS is set
  198.                         to the caller's data segment.
  199.  
  200.                         A program or DLL can only contain dynamic
  201.                         links to executable files that have this
  202.                         library module flag set. One program cannot
  203.                         dynamic-link to another program.
  204.  
  205.     0Eh     DW  Segment number of automatic data segment.
  206.                 This value is set to zero if SINGLEDATA and
  207.                 MULTIPLEDATA flag bits are clear, NOAUTODATA is
  208.                 indicated in the flags word.
  209.  
  210.                 A Segment number is an index into the module's segment
  211.                 table. The first entry in the segment table is segment
  212.                 number 1.
  213.  
  214.     10h     DW  Initial size, in bytes, of dynamic heap added to the
  215.                 data segment. This value is zero if no initial local
  216.                 heap is allocated.
  217.  
  218.     12h     DW  Initial size, in bytes, of stack added to the data
  219.                 segment. This value is zero to indicate no initial
  220.                 stack allocation, or when SS is not equal to DS.
  221.  
  222.     14h     DD  Segment number:offset of CS:IP.
  223.  
  224.     18h     DD  Segment number:offset of SS:SP.
  225.                 If SS equals the automatic data segment and SP equals
  226.                 zero, the stack pointer is set to the top of the
  227.                 automatic data segment just below the additional heap
  228.                 area.
  229.  
  230.                     +--------------------------+
  231.                     | additional dynamic heap  |
  232.                     +--------------------------+ <- SP
  233.                     |    additional stack      |
  234.                     +--------------------------+
  235.                     | loaded auto data segment |
  236.                     +--------------------------+ <- DS, SS
  237.  
  238.     1Ch     DW  Number of entries in the Segment Table.
  239.  
  240.     1Eh     DW  Number of entries in the Module Reference Table.
  241.     20h     DW  Number of bytes in the Non-Resident Name Table.
  242.  
  243.     22h     DW  Segment Table file offset, relative to the beginning
  244.                 of the segmented EXE header.
  245.  
  246.     24h     DW  Resource Table file offset, relative to the beginning
  247.                 of the segmented EXE header.
  248.  
  249.     26h     DW  Resident Name Table file offset, relative to the
  250.                 beginning of the segmented EXE header.
  251.  
  252.     28h     DW  Module Reference Table file offset, relative to the
  253.                 beginning of the segmented EXE header.
  254.  
  255.     2Ah     DW  Imported Names Table file offset, relative to the
  256.                 beginning of the segmented EXE header.
  257.  
  258.     2Ch     DD  Non-Resident Name Table offset, relative to the
  259.                 beginning of the file.
  260.  
  261.     30h     DW  Number of movable entries in the Entry Table.
  262.  
  263.     32h     DW  Logical sector alignment shift count, log(base 2) of
  264.                 the segment sector size (default 9).
  265.  
  266.     34h     DW  Number of resource entries.
  267.  
  268.     36h     DB  Executable type, used by loader.
  269.                   02h = WINDOWS
  270.  
  271.     37h-3Fh DB  Reserved, currently 0's.
  272.  
  273.  
  274. ======================================================================
  275.                             SEGMENT TABLE
  276. ======================================================================
  277.  
  278. The  segment table contains an entry  for each segment in the executable
  279. file.  The number of segment table  entries are defined in the segmented
  280. EXE  header.  The first entry in the  segment table is segment number 1.
  281. The following is the structure of a segment table entry.
  282.  
  283.    Size Description
  284.    ---- -----------
  285.  
  286.    DW   Logical-sector offset (n byte) to the contents of the segment
  287.         data, relative to the beginning of the file. Zero means no
  288.         file data.
  289.  
  290.    DW   Length of the segment in the file, in bytes. Zero means 64K.
  291.  
  292.    DW   Flag word.
  293.         0007h = TYPE_MASK  Segment-type field.
  294.         0000h = CODE       Code-segment type.
  295.         0001h = DATA       Data-segment type.
  296.         0010h = MOVEABLE   Segment is not fixed.
  297.         0040h = PRELOAD    Segment will be preloaded; read-only if
  298.                            this is a data segment.
  299.         0100h = RELOCINFO  Set if segment has relocation records.
  300.         F000h = DISCARD    Discard priority.
  301.  
  302.    DW   Minimum allocation size of the segment, in bytes. Total size
  303.         of the segment. Zero means 64K.
  304.  
  305.  
  306. ======================================================================
  307.                             RESOURCE TABLE
  308. ======================================================================
  309.  
  310. The  resource  table follows the segment  table and contains entries for
  311. each  resource in the executable file. The resource table consists of an
  312. alignment  shift  count,  followed by a  table  of resource records. The
  313. resource  records  define  the  type ID  for  a  set  of resources. Each
  314. resource  record  contains  a table of  resource  entries of the defined
  315. type.  The  resource  entry defines the resource  ID  or name ID for the
  316. resource.  It  also defines the location  and  size of the resource. The
  317. following describes the contents of each of these structures:
  318.  
  319.    Size Description
  320.    ---- -----------
  321.  
  322.    DW   Alignment shift count for resource data.
  323.  
  324.    A table of resource type information blocks follows. The following
  325.    is the format of each type information block:
  326.  
  327.         DW  Type ID. This is an integer type if the high-order bit is
  328.             set (8000h); otherwise, it is an offset to the type string,
  329.             the offset is relative to the beginning of the resource
  330.             table. A zero type ID marks the end of the resource type
  331.             information blocks.
  332.  
  333.         DW  Number of resources for this type.
  334.  
  335.         DD  Reserved.
  336.  
  337.         A table of resources for this type follows. The following is
  338.         the format of each resource (8 bytes each):
  339.  
  340.             DW  File offset to the contents of the resource data,
  341.                 relative to beginning of file. The offset is in terms
  342.                 of the alignment shift count value specified at
  343.                 beginning of the resource table.
  344.  
  345.             DW  Length of the resource in the file (in bytes).
  346.  
  347.             DW  Flag word.
  348.                 0010h = MOVEABLE  Resource is not fixed.
  349.                 0020h = PURE      Resource can be shared.
  350.                 0040h = PRELOAD   Resource is preloaded.
  351.  
  352.             DW  Resource ID. This is an integer type if the high-order
  353.                 bit is set (8000h), otherwise it is the offset to the
  354.                 resource string, the offset is relative to the
  355.                 beginning of the resource table.
  356.  
  357.             DD  Reserved.
  358.  
  359. Resource  type  and name strings are stored  at  the end of the resource
  360. table.  Note  that  these strings are  NOT  null terminated and are case
  361. sensitive.
  362.  
  363.    DB   Length of the type or name string that follows. A zero value
  364.         indicates the end of the resource type and name string, also
  365.         the end of the resource table.
  366.  
  367.    DB   ASCII text of the type or name string.
  368.  
  369.  
  370. ======================================================================
  371.                          RESIDENT-NAME TABLE
  372. ======================================================================
  373.  
  374. The  resident-name  table follows the  resource table, and contains this
  375. module's  name string and resident  exported procedure name strings. The
  376. first string in this table is this module's name. These name strings are
  377. case-sensitive  and are not null-terminated. The following describes the
  378. format of the name strings:
  379.  
  380.    Size Description
  381.    ---- -----------
  382.  
  383.    DB   Length of the name string that follows. A zero value indicates
  384.         the end of the name table.
  385.  
  386.    DB   ASCII text of the name string.
  387.  
  388.    DW   Ordinal number (index into entry table). This value is ignored
  389.         for the module name.
  390.  
  391.  
  392. ======================================================================
  393.                         MODULE-REFERENCE TABLE
  394. ======================================================================
  395.  
  396. The  module-reference table follows the  resident-name table. Each entry
  397. contains an offset for the module-name string within the imported- names
  398. table; each entry is 2 bytes long.
  399.  
  400.    Size Description
  401.    ---- -----------
  402.  
  403.    DW   Offset within Imported Names Table to referenced module name
  404.         string.
  405.  
  406.  
  407. ======================================================================
  408.                          IMPORTED-NAME TABLE
  409. ======================================================================
  410.  
  411. The  imported-name table follows the  module-reference table. This table
  412. contains  the  names of modules and  procedures that are imported by the
  413. executable  file. Each entry is composed of a 1-byte field that contains
  414. the  length  of  the string, followed  by  any number of characters. The
  415. strings are not null-terminated and are case sensitive.
  416.  
  417.    Size Description
  418.    ---- -----------
  419.  
  420.    DB   Length of the name string that follows.
  421.  
  422.    DB   ASCII text of the name string.
  423.  
  424.  
  425. ======================================================================
  426.                              ENTRY TABLE
  427. ======================================================================
  428.  
  429. The  entry  table follows the  imported-name  table. This table contains
  430. bundles  of  entry-point definitions. Bundling is  done to save space in
  431. the  entry  table.  The  entry table  is  accessed  by an ordinal value.
  432. Ordinal  number  one  is defined to index  the  first entry in the entry
  433. table.  To find an entry point, the  bundles are scanned searching for a
  434. specific  entry  point  using an ordinal  number.  The ordinal number is
  435. adjusted  as  each bundle is checked.  When the bundle that contains the
  436. entry  point  is found, the ordinal number  is multiplied by the size of
  437. the bundle's entries to index the proper entry.
  438.  
  439. The  linker  forms  bundles in the most  dense  manner it can, under the
  440. restriction that it cannot reorder entry points to improve bundling. The
  441. reason  for this restriction is that other .EXE files may refer to entry
  442. points  within  this  bundle  by  their  ordinal  number.  The following
  443. describes the format of the entry table bundles.
  444.  
  445.    Size Description
  446.    ---- -----------
  447.  
  448.    DB   Number of entries in this bundle. All records in one bundle
  449.         are either moveable or refer to the same fixed segment. A zero
  450.         value in this field indicates the end of the entry table.
  451.  
  452.    DB   Segment indicator for this bundle. This defines the type of
  453.         entry table entry data within the bundle. There are three
  454.         types of entries that are defined.
  455.  
  456.         000h = Unused entries. There is no entry data in an unused
  457.                bundle. The next bundle follows this field. This is
  458.                used by the linker to skip ordinal numbers.
  459.  
  460.         001h-0FEh = Segment number for fixed segment entries. A fixed
  461.                segment entry is 3 bytes long and has the following
  462.                format.
  463.  
  464.             DB  Flag word.
  465.                 01h = Set if the entry is exported.
  466.                 02h = Set if the entry uses a global (shared) data
  467.                       segments.
  468.                       The first assembly-language instruction in the
  469.                       entry point prologue must be "MOV AX,data
  470.                       segment number". This may be set only for
  471.                       SINGLEDATA library modules.
  472.  
  473.             DW  Offset within segment to entry point.
  474.  
  475.         0FFH = Moveable segment entries. The entry data contains the
  476.                segment number for the entry points. A moveable segment
  477.                entry is 6 bytes long and has the following format.
  478.  
  479.             DB  Flag word.
  480.                 01h = Set if the entry is exported.
  481.                 02h = Set if the entry uses a global (shared) data
  482.                       segments.
  483.  
  484.             INT 3FH.
  485.  
  486.             DB  Segment number.
  487.  
  488.             DW  Offset within segment to entry point.
  489.  
  490.  
  491. ======================================================================
  492.                         NONRESIDENT-NAME TABLE
  493. ======================================================================
  494.  
  495. The  nonresident-name  table  follows the  entry  table,  and contains a
  496. module  description and nonresident exported procedure name strings. The
  497. first  string in this table is  a module description. These name strings
  498. are  case-sensitive and are not null-terminated. The name strings follow
  499. the same format as those defined in the resident name table.
  500.  
  501.  
  502. ======================================================================
  503.                            PER SEGMENT DATA
  504. ======================================================================
  505.  
  506. The  location and size of the per-segment data is defined in the segment
  507. table  entry  for the segment. If  the segment has relocation fixups, as
  508. defined  in  the  segment table entry  flags,  they  directly follow the
  509. segment data in the file. The relocation fixup information is defined as
  510. follows:
  511.  
  512.  
  513.    Size Description
  514.    ---- -----------
  515.  
  516.    DW   Number of relocation records that follow.
  517.  
  518.    A table of relocation records follows. The following is the format
  519.    of each relocation record.
  520.  
  521.         DB  Source type.
  522.             0Fh = SOURCE_MASK
  523.             00h = LOBYTE
  524.             02h = SEGMENT
  525.             03h = FAR_ADDR (32-bit pointer)
  526.             05h = OFFSET (16-bit offset)
  527.  
  528.         DB  Flags byte.
  529.             03h = TARGET_MASK
  530.             00h = INTERNALREF
  531.             01h = IMPORTORDINAL
  532.             02h = IMPORTNAME
  533.             03h = OSFIXUP
  534.             04h = ADDITIVE
  535.  
  536.         DW  Offset within this segment of the source chain.
  537.             If the ADDITIVE flag is set, then target value is added to
  538.             the source contents, instead of replacing the source and
  539.             following the chain. The source chain is an 0FFFFh
  540.             terminated linked list within this segment of all
  541.             references to the target.
  542.  
  543. The target value has four types that are defined in the flag byte field.
  544. The following are the formats for each target type:
  545.  
  546.         INTERNALREF
  547.  
  548.             DB  Segment number for a fixed segment, or 0FFh for a
  549.                 movable segment.
  550.  
  551.             DB  0
  552.  
  553.             DW  Offset into segment if fixed segment, or ordinal
  554.                 number index into Entry Table if movable segment.
  555.  
  556.         IMPORTNAME
  557.  
  558.             DW  Index into module reference table for the imported
  559.                 module.
  560.  
  561.             DW  Offset within Imported Names Table to procedure name
  562.                 string.
  563.  
  564.         IMPORTORDINAL
  565.  
  566.             DW  Index into module reference table for the imported
  567.                 module.
  568.             DW  Procedure ordinal number.
  569.  
  570.         OSFIXUP
  571.  
  572.             DW  Operating system fixup type.
  573.                 Floating-point fixups.
  574.                 0001h = FIARQQ, FJARQQ
  575.                 0002h = FISRQQ, FJSRQQ
  576.                 0003h = FICRQQ, FJCRQQ
  577.                 0004h = FIERQQ
  578.                 0005h = FIDRQQ
  579.                 0006h = FIWRQQ
  580.  
  581.             DW  0
  582.  
  583. ======================================================================
  584.  
  585. Microsoft is a registered trademark and Windows is a trademark of
  586. Microsoft Corporation.
  587.  
  588. Additional reference words: 3.0
  589.  
  590.                             \\\\\//
  591.                            @ 0   0 @
  592. <-------------------oOOO------(_)--------OOOo--------------------->
  593.                        dBsee Product Manager
  594.                            Matteo Baccan
  595.                       mailto:baccan@isanet.it
  596.                        mailto:guru@dbsee.com
  597.                       http://matteo.isanet.it
  598.  
  599.         Get a free copy of dBsee from http://www.dbsee.com
  600.  
  601.      Current releases : dBsee 4.5, dBsee ++ 5.04, dBsee400 4.5
  602.  
  603.            Current betas : dBsee ++ 6, dBsee400 ++
  604. <------------------------oooO----------Oooo----------------------->
  605.